package com.study.mapstruct.controller;

import com.study.mapstruct.common.GenderEnum;
import com.study.mapstruct.entity.Course;
import com.study.mapstruct.entity.Student;
import com.study.mapstruct.entity.StudentVO;
import com.study.mapstruct.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class DemoController {

    @Autowired
    StudentMapper studentMapper;

    @GetMapping("/demo")
    public void demo() throws Exception {
        Student student = Student.builder()
                .name("小明")
                .age(6)
                .gender(GenderEnum.Male)
                .height(121.1)
                .birthday(new Date())
                .build();
        System.out.println("转换前：" + student);
        // 最简单转换
        StudentVO studentVO = studentMapper.student2StudentVO(student);
        System.out.println("最简单转换后：" + studentVO);
        // list转换
        List<Student> list = new ArrayList<>();
        list.add(student);
        List<StudentVO> result = studentMapper.students2StudentVOs(list);
        System.out.println("集合转换后：" + result);
        // 多对象转换到一个对象
        Course course = Course.builder().id(1L).courseName("语文").build();
        StudentVO studentVO2 = studentMapper.studentAndCourse2StudentVO(student, course);
        System.out.println("多对象转换成单一对象：" + studentVO2);
        // expression属性的使用,用于类型转换等
        StudentVO studentVO3 = StudentVO.builder()
                .name("张三")
                .gender("女")
                .age(18)
                .height(180.0)
                .birthday("2021-01-12 17:07:05")
                .build();
        Student student2 = studentMapper.studentVO2Student(studentVO3);
        System.out.println("expression表达式转换后：" + student2);
        // 更新现有的Bean实例
        studentVO3 = studentMapper.updateStudentVOFromStudent(student, studentVO3);
        System.out.println("更新后的对象：" + studentVO3);
        //继承反转配置
        Student student3 = studentMapper.studentVO2StudentInverse(studentVO3);
        System.out.println("继承反转配置后的对象：" + student3);
    }

}
